package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.MemberAddr;
import com.powernode.mapper.MemberAddrMapper;
import com.powernode.service.MemberAddrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.MemberAddrServiceImpl")
public class MemberAddrServiceImpl extends ServiceImpl<MemberAddrMapper, MemberAddr> implements MemberAddrService {

    @Autowired
    private MemberAddrMapper memberAddrMapper;

    /**
     * 查询会员收货地址列表
     *
     * @param openId
     * @return
     */
    @Override
    @Cacheable(key = "#openId")
    public List<MemberAddr> queryMemberAddrListByOpenId(String openId) {
        return memberAddrMapper.selectList(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                .eq(MemberAddr::getStatus, 1)
                .orderByDesc(MemberAddr::getCommonAddr, MemberAddr::getCreateTime));
    }

    /**
     * 新增会员收货地址
     *
     * @param memberAddr
     * @return
     */

    @Override
    @CacheEvict(key = "#openId")
    public Boolean saveMemberAddr(MemberAddr memberAddr, String openId) {
        //补充收货地址信息
        memberAddr.setCommonAddr(0);
        memberAddr.setStatus(1);
        memberAddr.setCreateTime(new Date());
        memberAddr.setUpdateTime(new Date());
        memberAddr.setOpenId(openId);
        //根据会员openId查询会员收货地址列表
        Long count = memberAddrMapper.selectCount(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId));
        //判断会员是否有收货地址
        if (0 == count) {
            //说明: 当前的会员是第一次新增收货地址
            memberAddr.setCommonAddr(1);
        }
        return memberAddrMapper.insert(memberAddr) > 0;
    }

    /**
     * 修改会员收货地址信息
     *
     * @param memberAddr
     * @param openId
     * @return
     */
    @Override
    @CacheEvict(key = "#openId")
    public Boolean modifyMemberAddrInfo(MemberAddr memberAddr, String openId) {
        memberAddr.setUpdateTime(new Date());
        return memberAddrMapper.updateById(memberAddr) > 0;
    }


    /**
     * 删除会员收货地址
     *
     * @param addrId
     * @param openId
     * @return
     */
    @Override
    @CacheEvict(key = "#openId")
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeMemberAddrById(Long addrId, String openId) {
        //根据收货地址id查询收货地址对象
        MemberAddr memberAddr = memberAddrMapper.selectById(addrId);
        //判断是否为默认地址
        if (memberAddr.getCommonAddr().equals(1)) {
            List<MemberAddr> memberAddrs = memberAddrMapper.selectList(new LambdaQueryWrapper<MemberAddr>()
                    .eq(MemberAddr::getOpenId, openId)
                    .eq(MemberAddr::getCommonAddr, 0)
                    .orderByDesc(MemberAddr::getCreateTime));
            //判断非默认收货地址是否有值
            if (CollectionUtil.isNotEmpty(memberAddrs) && memberAddrs.size() != 0) {
                MemberAddr newDefaultMemberAddr = memberAddrs.get(0);
                newDefaultMemberAddr.setCommonAddr(1);
                newDefaultMemberAddr.setUpdateTime(new Date());
                memberAddrMapper.updateById(newDefaultMemberAddr);
            }
        }
        //当前删除的收货地址非默认收货地址
        return memberAddrMapper.deleteById(addrId) > 0;
    }

    /**
     * 设置默认收货地址
     *
     * @param newAddrId
     * @param openId
     * @return
     */
    @Override
    @CacheEvict(key = "#openId")
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyMemberDefaultAddr(Long newAddrId, String openId) {
        //根据收货地址标识查询收货地址对象
        MemberAddr newDefaultMemberAddr = memberAddrMapper.selectById(newAddrId);
        //判断当前新的默认地址是否为原有的默认收货地址
        if (newDefaultMemberAddr.getCommonAddr().equals(1)) {
            //是: 结束
            return true;
        }
        //不是: 将之前的默认收货地址设置为非默认收货地址,并更新当前新的默认收货地址
        MemberAddr oldDefaultMemberAddr = new MemberAddr();
        oldDefaultMemberAddr.setCommonAddr(0);
        oldDefaultMemberAddr.setUpdateTime(new Date());
        memberAddrMapper.update(oldDefaultMemberAddr, new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId));
        //将当前的默认地址设置的新的默认收货地址
        newDefaultMemberAddr.setCommonAddr(1);
        newDefaultMemberAddr.setUpdateTime(new Date());
        return memberAddrMapper.updateById(newDefaultMemberAddr) > 0;
    }
}
